package it.nesimuge.score.gui.gare;

import it.lapomuge.db.dao.DbManagerGare;
import it.lapomuge.db.entity.Gara;
import it.lapomuge.db.entity.Manche;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

public class JDialogGaraSingola extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private Gara gara;
	JTextPane textPane = new JTextPane();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JDialogGaraSingola dialog = new JDialogGaraSingola();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public JDialogGaraSingola(){
		this(new Gara());
	}

	/**
	 * Create the dialog.
	 */
	public JDialogGaraSingola(Gara gara) {
		this.gara = gara;
		this.setModal(true);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{121, 69, 116, 0};
		gbl_contentPanel.rowHeights = new int[]{22, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel lblNomeGara = new JLabel("Descrizione Gara:");
			GridBagConstraints gbc_lblNomeGara = new GridBagConstraints();
			gbc_lblNomeGara.anchor = GridBagConstraints.WEST;
			gbc_lblNomeGara.insets = new Insets(0, 0, 5, 5);
			gbc_lblNomeGara.gridx = 0;
			gbc_lblNomeGara.gridy = 0;
			contentPanel.add(lblNomeGara, gbc_lblNomeGara);
		}
		{
			GridBagConstraints gbc_textPane = new GridBagConstraints();
			gbc_textPane.gridwidth = 3;
			gbc_textPane.insets = new Insets(0, 0, 0, 5);
			gbc_textPane.fill = GridBagConstraints.BOTH;
			gbc_textPane.gridx = 0;
			gbc_textPane.gridy = 1;
			contentPanel.add(textPane, gbc_textPane);
			textPane.setText(gara.getNome());
		}
		{
			JPanel buttonPane = new JPanel();
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			buttonPane.setLayout(new GridLayout(0, 2, 0, 0));
			{
				JButton cancelButton = new JButton("Annulla");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
			{
				JButton okButton = new JButton("Avanti...");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						mouseClick();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}
	protected void mouseClick() {
		this.gara.setNome(textPane.getText());
//		this.gara.setStato(CodiceStatoGara.NON_DISPUTATA.getCodice());
		DbManagerGare.getInstance().save(Gara.class, gara);
		
		this.dispose();
		Manche[] manches = DbManagerGare.getInstance().getManches(gara);
		Manche m = null;
		if(manches.length > 0){
			m = manches[0];
		}else{
			m = new Manche();
			m.setOrdine(1);
			m.setGara(gara);
			m.setStato(gara.getStato());
			
		}
		
		
		jDialogManche j = new jDialogManche(m);
		j.setVisible(true);
		
	}

}
